package com.igeek.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeek.domain.Property;
import com.igeek.service.PropertyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author spike
 * @since 2022-01-12
 */
@RestController
@Slf4j
public class PropertyController {
    @Autowired
    private PropertyService propertyService;
    //POST http://localhost:8080/properties
    @PostMapping("/properties")
    public void addProperty(@RequestBody Property property){
        log.info("property2:"+property);
        //id=0, cid=null, name=bbb, category=Category(id=83, name=null)
        //category的id赋给Property的cid
        property.setCid(property.getCategory().getId());
        propertyService.save(property);
    }

    //http://localhost:8080/categories/83/properties?start=0
    @GetMapping("/categories/{cid}/properties")
    public PageInfo<Property> propertyList(@PathVariable("cid") Integer cid,
       @RequestParam(value = "start",required = true,defaultValue = "1") Integer start,
       @RequestParam(value = "size",required = true,defaultValue = "5") Integer size){
        PageHelper.startPage(start,size);
        QueryWrapper<Property> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",cid);
        List<Property> propertyList = propertyService.list(wrapper);
        PageInfo<Property> pageInfo = new PageInfo<>(propertyList);
        log.info("pageInfo1:"+pageInfo);
        return pageInfo;
    }
}

